<%!
	// 推送消息
	public void pushMsg(Input input, String title, String content) {
		ManageUser user = (ManageUser)input.getRequest().getSession().getAttribute("user");
		title = user.getName() + title;
		Messages pushTool = Messages.with(CORPID, CORPSECRET);
		NewsMessage jsonMsg = new NewsMessage();
		com.tianxiabuyi.weixin.common.message.News news = new com.tianxiabuyi.weixin.common.message.News();
		Article article = new Article();
		article.setTitle(title);
		article.setDescription(content);
		List<Article> list = new ArrayList<>();
		list.add(article);
		news.setArticles(list);
		jsonMsg.setNews(news);
		jsonMsg.setAgentId(18);
		jsonMsg.setToUser("yanghx");
		jsonMsg.setToTag("4");
		pushTool.send(jsonMsg);
	}
	
	// 所有医院
	public Message hospitals(Input input) {
		Message msg = new Message();
		List<Map> list = QBuilder.db("basedb").select("new map(name as name, hospital as hospital_id)").from("Hospital").where(new Where("status=", STATUS_ENABLE)).query().getResultObj();
		msg.put("data", list);
		return msg;
	}
	
	// 所有医院
	public String hospitalNameByHospital(Integer hospital) {
		Message msg = new Message();
		List<Map> list = QBuilder.db("basedb").select("new map(name as name, hospital as hospital_id)").from("Hospital").where(new Where("status=", STATUS_ENABLE).and("hospital=", hospital)).query().getResultObj();
		if (isEmpty(list))
			return "未知医院";
		return list.get(0).get("name") + "";
	}
	
	// 获取项目列表
	public Message projects(Input input) {
		Message msg = new Message();
		Integer pageNum = input.getInt("page", 1);; 		// 页码
		Integer itemCount = input.getInt("item_count", 9);  // 每一页条目
		Integer hospitalId = input.getInt("hospital_id", -1);    // 医院编号
		if (pageNum <= 0 || itemCount <= 0) {
			return new Message(30041, "参数不合法，请输入正确的页码或限值");
		}
		String startTime = input.getDesc("start_time", "");
		String endTime = input.getDesc("end_time", "");
		String principal = input.getDesc("principal", "");
		String saleLeader = input.getDesc("sale_leader", "");
		String state = input.getDesc("state", "-1");
		String order = input.getDesc("order", "asc");
		Where where = new Where("i.status=", STATUS_ENABLE).and("p.hospital=i.hospital");
		if (!"".equals(startTime))
			where.and("i.start_time>", DateUtil.parseDate(startTime).getTime());
		if (!"".equals(startTime))
			where.and("i.end_time<", DateUtil.parseDate(endTime).getTime());
		if (!"".equals(principal))
			where.and("i.principal=", principal);
		if (!"".equals(saleLeader))
			where.and("i.sale_leader=", saleLeader);
		if (hospitalId != -1)
			where.and("i.hospital=", hospitalId);
		if (!"-1".equals(state))
			where.and("i.state=", state);
		List<Map> ranks = QBuilder.db("basedb").select("new map(i.id as id, i.name as item_name, i.principal as principal, i.version_type as version_type, i.item_type as item_type, i.design_leader as design_leader, i.developer as developer, i.sale_leader as sale_leader, i.sale_manager as sale_manager, concat(FROM_UNIXTIME(i.start_time/1000, '%Y-%m-%d'), '') as start_time, concat(FROM_UNIXTIME(i.end_time/1000, '%Y-%m-%d'), '') as end_time, i.description as description, i.remark as remark, p.name as hospital_name, i.status as status, i.state as state)").from("project as i, Hospital as p").where(where).page(itemCount, pageNum).order("i.end_time", order).query().getResultObj();
		List<Long> rankCount = QBuilder.db("basedb").select("count(*)").from("project as i, Hospital as p").where(where).order("i.end_time", order).query().getResultObj();
		Long size = rankCount.get(0);
		Map resultMap = new HashMap();
		resultMap.put("rank", ranks);
		resultMap.put("current_page", pageNum);
		resultMap.put("page_count",  size%itemCount == 0?(size/itemCount):(size/itemCount + 1));
		resultMap.put("count", size);
		msg.put("data", resultMap);
		return msg;
	}

	// 所有主要负责人和销售负责人
	public Message principals(Input input) {
		Message msg = new Message();
		List<String> ranks = QBuilder.db("basedb").select("distinct i.principal").from("project as i").where(new Where()).query().getResultObj();
		msg.put("principals", ranks);
		List<String> saleLeaders = QBuilder.db("basedb").select("distinct i.sale_leader").from("project as i").where(new Where()).query().getResultObj();
		msg.put("sale_leaders", saleLeaders);
		return msg;
	}

	// 展现项目详情
	public Message showProject(Input input) {
		Message msg = new Message();
		Integer id = input.getInt("id");
		List<Map> itemList = QBuilder.db("basedb").queryHql("select new map(i.id as id, i.name as name, i.hospital as hospital, h.name as hospital_name) from project as i, Hospital as h where i.hospital = h.hospital and i.id = " + id + " and i.status = " + STATUS_ENABLE);
		if (isEmpty(itemList))
			return new Message(30041, "没有这个工程");
		msg.put("data", itemList.get(0));
		return msg;
	}


	// 添加项目
	public Message createProject(Input input) {
		Message msg = new Message();
		Integer hospitalId = input.getInt("hospital_id");
		Map project = new HashMap();
		project.put("hospital", hospitalId);
		project.put("name", input.getDesc("name", ""));
		project.put("principal", input.getDesc("principal", ""));
		project.put("version_type", input.getDesc("version_type", ""));
		project.put("item_type", input.getDesc("item_type", ""));
		project.put("design_leader", input.getDesc("design_leader", ""));
		project.put("developer", input.getDesc("developer", ""));
		project.put("sale_leader", input.getDesc("sale_leader", ""));
		project.put("sale_manager", input.getDesc("sale_manager", ""));
		project.put("start_time", DateUtil.parseDate(input.getDesc("start_time", DateUtil.formatDate(new Date()))).getTime());
		project.put("end_time", DateUtil.parseDate(input.getDesc("end_time", DateUtil.formatDate(new Date()))).getTime());
		project.put("description", input.getDesc("description", ""));
		project.put("remark", input.getDesc("remark", ""));
		project.put("state", input.getDesc("state", ""));
		project.put("create_time", System.currentTimeMillis());
		project.put("status", 1);
		project.put("json", "{}");
		Serializable key = DBTools.db("basedb").addItem("project", project);
		if (key == null) {
			msg.setErrcodeAndErrmsg(30041, "添加失败");
		} else {
			msg.put("map", project);
			String title = "创建了[" + hospitalNameByHospital(hospitalId) + input.getDesc("name", "") + "]项目";
			String content = "开始时间: " + input.getDesc("start_time") + "\n";
			content = content + "结束时间: " + input.getDesc("end_time") + "\n";
			content = content + "主要负责人: " + input.getDesc("principal") + "\n";
			content = content + "销售负责人: " + input.getDesc("sale_leader") + "\n";
			content = content + "包含: " + input.getDesc("version_type", "") + "\n";
			content = content + "项目类型: " + input.getDesc("item_type", "") + "\n";
			pushMsg(input, title, content);
		}
		return msg;
	}

	// 删除项目
	public Message deleteProject(Input input) {
		Message msg = new Message();
		int id = input.getInt("id");
		List<Map> list = QBuilder.db("basedb").queryHql("from project where id = " + id + " and status = " + STATUS_ENABLE);
		if(list.size() == 0)
			return new Message(30041, "没有这个工程");
		Map project = list.get(0);
		project.put("status", STATUS_DISABLE);
		DBTools.db("basedb").modItem("project", project);
		String title = "删除了[" + hospitalNameByHospital((Integer)project.get("hospital")) + project.get("name") + "]项目";
		pushMsg(input, title, title);
		return msg;
	}

	// 修改项目
	public Message updateProject(Input input) {
		Message msg = new Message();
		int id = input.getInt("id");
				Integer hospitalId = input.getInt("hospital_id");
		List<Map> list = QBuilder.db("basedb").queryHql("from project where id = " + id + " and status = " + STATUS_ENABLE);
		if(list.size() == 0)
			return new Message(30041, "没有这个工程");
		Map project = list.get(0);
		project.put("hospital", hospitalId);
		project.put("name", input.getDesc("name", ""));
		project.put("principal", input.getDesc("principal", ""));
		project.put("version_type", input.getDesc("version_type", ""));
		project.put("item_type", input.getDesc("item_type", ""));
		project.put("design_leader", input.getDesc("design_leader", ""));
		project.put("developer", input.getDesc("developer", ""));
		project.put("sale_leader", input.getDesc("sale_leader", ""));
		project.put("sale_manager", input.getDesc("sale_manager", ""));
		project.put("start_time", DateUtil.parseDate(input.getDesc("start_time", DateUtil.formatDate(new Date()))).getTime());
		project.put("end_time", DateUtil.parseDate(input.getDesc("end_time", DateUtil.formatDate(new Date()))).getTime());
		project.put("description", input.getDesc("description", ""));
		project.put("remark", input.getDesc("remark", ""));
		project.put("create_time", System.currentTimeMillis());
		project.put("situation", input.getDesc("situation", ""));
		project.put("state", input.getDesc("state", ""));
		DBTools.db("basedb").modItem("project", project);
		String title = "更改了[" + hospitalNameByHospital(hospitalId) + project.get("name") + "]项目";
		pushMsg(input, title, title);
		return msg;
	}
%>
